/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.purpose.mapper;

import java.util.LinkedHashMap;
import java.util.List;

import com.koron.system.domain.DictData;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.koron.ebs.mybatis.EnvSource;

import com.koron.purpose.domain.JobsTemplateCertificate;


public interface JobsTemplateCertificateMapper {

    @Delete("delete from jobs_template_certificate where template_id=#{id}")
    int delete(@Param("id") String templateId);

    @Insert("<script>"
            + "insert into jobs_template_certificate(id,certificate_type_id,certificate_type_label,authorization,template_id) values"
            + "<foreach collection ='beanList' item='item' index='index' separator =','>"
            + "(#{item.id},#{item.certificateTypeId},#{item.certificateTypeLabel},#{item.authorization},#{item.templateId}) </foreach>"
            + "</script>")
    int batchInsert(@Param("beanList") List<JobsTemplateCertificate> list);

    @Select("select * from jobs_template_certificate where template_id=#{id}")
    List<JobsTemplateCertificate> queryByTid(@Param("id") String templateId);

    @Select("<script>"
            + "select "
            + "c. NAME AS '岗位工种',"
            + "<foreach collection ='dataList' item='data' index='index' separator =','>"
            + "MIN(CASE b.id WHEN #{data.id} THEN a.authorization ELSE 3 END ) AS #{data.dictLabel}"
            + "</foreach>"
            + " FROM jobs_template_certificate a"
            + " LEFT JOIN dict_data b ON a.certificate_type_id = b.id"
            + " LEFT JOIN jobs_template c ON a.template_id = c.id"
            + " WHERE b.dict_key = 'certificate_type'"
            + "<if test='templateName!=null and !templateName.isEmpty()'>"
            + " and c.name =#{templateName}"
            + "</if>"
            + "<if test='corpCode!=null and !corpCode.isEmpty()'>"
            + " and c.corp_code =#{corpCode}"
            + "</if>"
            + " GROUP BY a.template_id"
            + "<if test='start!=null'>"
            + " limit #{start},#{rows}"
            + "</if>"
            + "</script>")
    List<LinkedHashMap<String, Object>> queryDutyTemplateCetificate(@Param("corpCode") String corpCode, @Param("dataList") List<DictData> dataList, @Param("templateName") String templateName,
                                                                    @Param("start") Integer start, @Param("rows") Integer rows);
}
